package com.yeren.czbk.basic;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import javax.imageio.stream.FileImageInputStream;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;

import com.yeren.czbk.basic.entity.Tuser;



@SuppressWarnings("deprecation")
public class App {
	private static SessionFactory ssessionFactory;
	//初始化ssessionFactory
	static{
		Configuration cfg=new Configuration();
		cfg.configure("hibernate.cfg.xml");//读取配置文件
		ssessionFactory=cfg.buildSessionFactory();//根据配置生成session工厂
	}
//	@Test
	public void testSave() throws Exception{
		//读取图片文件
		InputStream in=new FileInputStream("d:/test.png");
		byte[] pic=new byte[in.available()];
		in.read(pic);
		in.close();
		
		Tuser user=new Tuser();
		user.setName("张三");
		user.setAccount("qqq");
		user.setPassword("qqqq");
		user.setAge(9);
		user.setPicture(pic);
		
		//保存
		Session session=ssessionFactory.openSession();//打开一个新的事务
		Transaction t =session.beginTransaction();//开始事务
		session.save(user);
		t.commit();//提交事务
		session.close();//关闭session，释放资源
		
	}
	
	@Test
	public void testGet() throws Exception{
		Session session=ssessionFactory.openSession();//打开一个新的事务
		Transaction t =session.beginTransaction();//开始事务
		Tuser user=(Tuser) session.get(Tuser.class, 1);
		
		OutputStream out=new FileOutputStream("d:/copy.png");
		out.write(user.getPicture());
		out.close();
		
		
		t.commit();//提交事务
		session.close();//关闭session，释放资源
		System.out.println(user.getAge());
	}
}
